package com.dtsz.m2.ui.base;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.VolleyError;
import com.dtsz.m2.R;
import com.dtsz.m2.provider.DataUtils;
import com.dtsz.m2.ui.model.PageVo;
import com.dtsz.m2.ui.view.LoadingDailog;
import com.dtsz.m2.utils.StatusBarUtils;
import com.dtsz.m2.volley.httpUtil.HCUParame;
import com.dtsz.m2.volley.httpUtil.HUCRequest;
import com.dtsz.m2.volley.httpUtil.interfase.HUCCallBackInterfase;


public class BaseActivity extends AppCompatActivity implements OnClickListener,
        HUCCallBackInterfase {
    // 网络请求接口类
    public HUCRequest hucRequestInterfase;
    // 网络请求构参接口类
    public HCUParame hucParameInterfase;

    public TextView tvActionbarTitle;
    View ivActionbarLeft;

    public PageVo pageVo = new PageVo();

    private LoadingDailog dialog1;

    //声明SharedPreferences，用来读取xml;
    private SharedPreferences sp;
    //声明SharedPreferences.Editor，用来修改xml里面的值。
    private SharedPreferences.Editor ed;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //StatusBarUtils.setWindowStatusBarColor(this, R.color.colorMain);
        LinearLayout linearLayout = findViewById(R.id.lay_transroot);
        if (linearLayout != null) {
            linearLayout.setPadding(0, StatusBarUtils.getStatusBarHeight(this), 0, 0);
        }
        StatusBarUtils.setTransparentStatusBar(this);
        initUtils();
        initView();
        initClick();
    }

    public void sendUrl(String url) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url + "?token=" + pageVo.getToken());
        intent.setData(content_url);
        startActivity(intent);
    }


    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 初始化当前视图
     */
    public void initView() {
        tvActionbarTitle = findViewById(R.id.tv_actionbar_title);
        ivActionbarLeft = findViewById(R.id.lay_actionbar_left);
    }

    /**
     * 绑定监听
     */
    public void initClick() {
        if (ivActionbarLeft != null) {
            ivActionbarLeft.setOnClickListener(this);
        }
    }

    /**
     * 初始化工具类
     */
    public void initUtils() {
        sp = getSharedPreferences("setting", MODE_PRIVATE);
        String token = sp.getString("tokenRSA", "token001");
        pageVo.setToken(token);
        if (!"token001".equals(token)) {
            DataUtils.setMyPayToken(this, token);
        }
        hucRequestInterfase = HUCRequest.getInstance();
        hucParameInterfase = HCUParame.getInstance();
    }


    public void setSharedPreferences(String key, String vlaue) {
        ed = sp.edit();
        ed.putString(key, vlaue);//保存token
        ed.commit();
    }

    public String getSharedPreferences(String key) {
        sp = getSharedPreferences("setting", MODE_PRIVATE);
        String token = sp.getString(key, "token001");
        return token;
    }

    public String getSharedPreferences(String key, String value) {
        sp = getSharedPreferences("setting", MODE_PRIVATE);
        String token = sp.getString(key, value);
        return token;
    }

    /**
     * 点击事件
     */
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_actionbar_left:
                finish();
                break;
        }
    }

    public void onSuccessfulBitampType(int inputType, Bitmap response) {
        // TODO Auto-generated method stub

    }

    public void onErrorResponse(int inputType, VolleyError response) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onErrorLogin(int inputType) {

    }

    public void onSuccessfulDB(int inputType, Object obj) {
        // TODO Auto-generated method stub
    }

    public void onSuccessfulStrType(int inputType, Object response) {
        // TODO Auto-generated method stub

    }

    public void showToast(String toastContent) {
        Toast.makeText(this, toastContent, Toast.LENGTH_SHORT).show();
    }

    public void sendIntent(Activity activity, Class myClass) {
        Intent intent = new Intent();
        intent.setClass(activity, myClass);
        startActivity(intent);
    }

    public void sendIntent(Activity activity, Class myClass, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(activity, myClass);
        startActivityForResult(intent, requestCode);
    }

    public void sendIntent(Activity activity, Class myClass, String value) {
        Intent intent = new Intent();
        intent.setClass(activity, myClass);
        intent.putExtra("URL", value);
        startActivity(intent);
    }

    public void loading() {
        LoadingDailog.Builder builder1 = new LoadingDailog.Builder(this)
                .setMessage("加载中...")
                .setCancelOutside(true)
                .setCancelable(true);
        if (dialog1 == null) {
            dialog1 = builder1.create();
            dialog1.show();
        }
        dialog1.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (dialog1 != null) {
                    hucRequestInterfase.cancelAll();
                    dialog1.dismiss();
                }
                dialog1 = null;
            }
        });
    }

    public void loadDismiss() {
        if (dialog1 != null) {
            dialog1.dismiss();
        }
        dialog1 = null;
    }
}
